package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.hsf.baseinf.service.InfExchangeService;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.callclient.HttpsCallClient;
import com.iwhalecloud.bss.kite.cucc.common.callclient.constant.CallClientEnum;
import com.iwhalecloud.bss.kite.cucc.service.inf.InfServiceHelper;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;

import com.ztesoft.bss.common.exception.BssException;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * 接口校验复用APN域名
 */
public class CheckApnNameAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Value("${interface.call.mode:hsf}")
    private String mode;
    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        Map checkApnReq = new HashMap();
        if(!KiteStringUtils.isEmpty(value)){
            checkApnReq.put("APN_NAME", value);
            Map uniBssBody = new HashMap();
            uniBssBody.put("CHECK_APN_REQ", checkApnReq);
            Map reqMap = new HashMap();
            reqMap.put("UNI_BSS_BODY", uniBssBody);
            reqMap.put("OP_CODE", "json_jike_orderCenter_checkApn");
            InfServiceHelper.adaptGray(reqMap, checkApnReq);
            Map result;
            if (KiteStringUtils.isEqual(mode, "http")) {
                result = HttpsCallClient.postObjMap(CallClientEnum.CHECK_APN_NAME.getCenterCode(),
                    CallClientEnum.CHECK_APN_NAME.getServiceCode(), reqMap, null);
            }
            else {
                InfExchangeService infExchangeService = SpringUtil.getBean("infExchangeService");
                String resultStr = infExchangeService.exchange(new JSONObject(reqMap));
                result = JSON.parseObject(resultStr, Map.class);
            }
            Map uniBssHead = (Map) result.get("UNI_BSS_HEAD");
            if (KiteMapUtils.isNotEmpty(uniBssHead)) {
                String respDesc = KiteMapUtils.getString(uniBssHead, "RESP_DESC");
                if (KiteStringUtils.isEqual("Success", respDesc)) {
                    Map bodyMap = (Map) result.get("UNI_BSS_BODY");
                    Map checkApnRsp = (Map) bodyMap.get("CHECK_APN_RSP");
                    String respCode = KiteMapUtils.getString(checkApnRsp, "RESP_CODE");
                    if (KiteStringUtils.isEqual("1001", respCode)) {
                        BssException.throwOut("复用APN域名校验", "复用APN域名校验", "填写的APN域名：'" + value + "'，不存在，请重新填写");
                    }
                }
                else {
                    BssException.throwOut("复用APN域名校验", "复用APN域名校验", "APN域名校验接口调用失败");
                }
            }
        }
    }
}
